#!/bin/sh

# Electrum ABC - lightweight eCash client
# Copyright (C) 2020 The Electrum ABC Developers
# Copyright (C) 2019-2020 Axel Gembe <derago@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set -e

if [ `uname` == "Darwin" ]; then
	realpath=grealpath
else
	realpath=realpath
fi

here=$(dirname $($realpath "$0"))
. "$here"/../base.sh # functions we use below (fail, et al)

SRCURL="https://noto-website-2.storage.googleapis.com/pkgs/NotoColorEmoji-unhinted.zip"
SRCNAME=$(basename $SRCURL)
SRCPATH="$here/$SRCNAME"

if ! [ -e "$SRCPATH" ] ; then
    info "Downloading source file"
    wget -O "$SRCPATH" "$SRCURL"
fi

(
    info "Unzipping source file"
    rm -rf "$here"/unzipped
    mkdir "$here"/unzipped
    cd "$here"/unzipped
    unzip "$SRCPATH"
)

SRCTTF="$here"/unzipped/NotoColorEmoji.ttf

info "Building subset of the souce file"
pyftsubset "$SRCTTF" --unicodes-file="$here"/glyphs.txt --output-file="$here"/ecsupplemental.ttf

rm -rf "$here"/unzipped

info "Converting font to ttx format"
ttx "$here"/ecsupplemental.ttf

# We name the font 'Segoe UI Emoji' because that will override the emoji
# font on Windows. Sadly Qt has no way to override font fallback so this
# is the only way to do this other than binary patching or recompiling Qt.
info "Renaming font to Segoe UI Emoji for Windows"
sed -e 's/Noto Color Emoji/Segoe UI Emoji/' \
    -e 's/NotoColorEmoji/SegoeUIEmoji/'\
    -e 's/noto-emoji/segoe-ui-emoji/' \
    < "$here"/ecsupplemental.ttx \
    > "$here"/ecsupplemental_win.ttx
# Make reproducible
touch -h -d '2000-11-11T11:11:11+00:00' "$here"/ecsupplemental_win.ttx
ttx -f -o "$here"/../../gui/qt/data/ecsupplemental_win.ttf "$here"/ecsupplemental_win.ttx
rm "$here"/ecsupplemental_win.ttx

# We make another font for Linux to prioritise it with fontconfig
info "Renaming font to EC Supplemental for Linux"
sed -e 's/Noto Color Emoji/EC Supplemental/' \
    -e 's/NotoColorEmoji/ECSupplemental/'\
    -e 's/noto-emoji/ec-supplemental/' \
    < "$here"/ecsupplemental.ttx \
    > "$here"/ecsupplemental_lnx.ttx
# Make reproducible
touch -h -d '2000-11-11T11:11:11+00:00' "$here"/ecsupplemental_lnx.ttx
ttx -f -o "$here"/../../gui/qt/data/ecsupplemental_lnx.ttf "$here"/ecsupplemental_lnx.ttx
rm "$here"/ecsupplemental_lnx.ttx

rm "$here"/ecsupplemental.ttx
rm "$here"/ecsupplemental.ttf
